package cm.server.controller;

import cm.server.dao.areaRepository;
import cm.server.entity.area;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/area_people")
@CrossOrigin()
public class areaController {
    @Autowired
    private areaRepository arearepository;

    //    查
    @RequestMapping(value = "/getAllByArea", method = RequestMethod.GET)
    @ResponseBody
    public List<area> getAllByArea(Integer area) {
        List<area> areas = new ArrayList<>();
//        类型不匹配
        areas = arearepository.findAllByArea(area);
        return areas;
//        return arearepository.findAllByArea(area);
    }

    @RequestMapping(value = "/getByArea", method = RequestMethod.GET)
    @ResponseBody
    public List<area> getByArea(int area) {
        List<area> areas = new ArrayList<>();
        areas = arearepository.findByArea(area);
        return areas;
    }

    @RequestMapping(value = "/getByDensity", method = RequestMethod.GET)
    @ResponseBody
    public List<area> getByDensity(int density) {
        List<area> areas = new ArrayList<>();
        areas = arearepository.findByDensity(density);
        return areas;
    }

    @RequestMapping(value = "/getByTime", method = RequestMethod.GET)
    @ResponseBody
    public List<area> getByTime(int time) {
        List<area> areas = new ArrayList<>();
        areas = arearepository.findByTime(time);
        return areas;
    }

    @RequestMapping(value = "/getByNum", method = RequestMethod.GET)
    @ResponseBody
    public List<area> getByNum(int num) {
        List<area> areas = new ArrayList<>();
        areas = arearepository.findByNum(num);
        return areas;
    }

//    改（在写）
//    @RequestMapping(value = "/putByDensity", method = RequestMethod.POST)
//    @ResponseBody
//    public void putByDensity(@RequestBody int density) {
//        arearepository.
//    }

    //    增
    @RequestMapping(value = "/addByArea", method = RequestMethod.POST)
    @ResponseBody
    public void addByArea(@RequestBody area obj) {
        arearepository.save(obj);
    }

    //    删
    @RequestMapping(value = "/deleteAllByArea", method = RequestMethod.DELETE)
    @ResponseBody
    @Transactional
    public void deleteAllByArea(int area) {
        arearepository.deleteAllByArea(area);
    }

    @RequestMapping(value = "deleteByArea", method = RequestMethod.DELETE)
    @ResponseBody
    @Transactional
    public void deleteByArea(int area) {
        arearepository.deleteByArea(area);
    }
}
